﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace iSmartCreatorApp
{
    /// <summary>
    /// Interaction logic for ImageAddWindow.xaml
    /// </summary>
    public partial class ImageAddWindow : Window
    {
        public ImageAddWindow()
        {
            InitializeComponent();
            BtnOk.IsEnabled = false;
            radioBtnUseAlt.IsChecked = true;
            chckbxBorder.IsEnabled = false;
            txtbxAltText.Focus();
        }

        //Button "Cancel" click
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //Enable button "OK" if text != empty
        private void TxtBxImageLocation_TextChanged(object sender, TextChangedEventArgs e)
        {
            BtnOk.IsEnabled = TxtBxImageLocation.Text != "";
        }

        //Button to search image on computer
        private void BtnSearchImage_Click(object sender, RoutedEventArgs e)
        {
            var openFile = new OpenFileDialog
                {
                    Filter = "Image Files(*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.gif;*.jpeg;*.png",
                    Title = "Choose Image"
                };
            var result = openFile.ShowDialog();

            if (result == true)
            {
                TxtBxImageLocation.Text = openFile.FileName;
            }
            //var openFile = new OpenFileDialog { Filter = "HTML Files (.html)|*.html", Title = "Open HTML File" };
        }

        //Button "OK" click
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            Paragraph paragraph = new Paragraph();
            FlowDocument flow = new FlowDocument();
            ArrayList list = new ArrayList();
            string alt;
            string img;
            if (radioBtnUseAlt.IsChecked == true && txtbxAltText.Text == "")
            {
                MessageBox.Show("You need alternative text for your image", "iSmart Creator", MessageBoxButton.OK,
                                MessageBoxImage.Warning);
                return;
            }
            alt = radioBtnDontUseAlt.IsChecked == true ? "" : txtbxAltText.Text;

            string[] mas = MainWindow.HtmlText.Split('\n');
            string body = FindBody(mas);
            if (txtbxLink.Text != "" && chckbxBorder.IsChecked == true)
            {
                img = "<a href=\"" + txtbxLink.Text + "\"><img style=\"border: 2px solid;\" alt=\""+alt+"\" src=\""+TxtBxImageLocation.Text+"\"></a>";
            }
            else if (txtbxLink.Text != "" && chckbxBorder.IsChecked == false)
            {
                img = "<a href=\"" + txtbxLink.Text + "\"><img alt=\"" + alt + "\" src=\"" + TxtBxImageLocation.Text + "\"></a>";
            }
            else
            {
                img = "<img alt=\"" + alt + "\" src=\"" + TxtBxImageLocation.Text + "\">";
            }
            body += img;
                
            //MainWindow.currentRTBHtml.Document.Blocks.Clear();

            foreach (var i in mas)
            {
                list.Add(i);
            }
            list = ClearBody(list);
            list = NewBody(list, body);
            mas = (string[]) list.ToArray(typeof (string));
            mas = MainWindow.GoldClear(mas);
            foreach (var i in mas)
            {
                paragraph.Inlines.Add(i + "\n");
            }
            flow.Blocks.Add(paragraph);
            MainWindow.currentRTBHtml.Document = flow;
            this.Close();
        }

        private void radioBtnDontUseAlt_Checked(object sender, RoutedEventArgs e)
        {
            txtbxAltText.IsEnabled = false;
        }

        private void radioBtnUseAlt_Checked(object sender, RoutedEventArgs e)
        {
            txtbxAltText.IsEnabled = true;
            txtbxAltText.Focus();
        }

        private string FindBody(string[] mas)
        {
            string str = "";

            for (int i = 0; i < mas.Length; i++)
            {
                if (mas[i] == "<body>")
                {
                    i++;
                    while (mas[i] != "</body>")
                    {
                        str += mas[i] + "\n";
                        i++;
                    }
                    break;
                }
            }
            return str;
        }

        private ArrayList ClearBody(ArrayList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ToString() == "<body>")
                {
                    i++;
                    while (list[i].ToString() != "</body>")
                    {
                        list.Remove(list[i]);
                    }
                    break;
                }
            }
            return list;
        }

        private ArrayList NewBody(ArrayList list, string body)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ToString() == "<body>")
                {
                    list.Insert(i + 1, body);
                    break;
                }
            }
            return list;
        }

        private void txtbxLink_TextChanged(object sender, TextChangedEventArgs e)
        {
            chckbxBorder.IsEnabled = txtbxLink.Text != "";
        }
    }
}
